package com.vancouver.knows.ui.fragment.citydiscount;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.pangu.util.StringUtil;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.view.ProjectTextAdapter;
import com.vancouver.knows.ui.view.SubProjectTextAdapter;
import com.vancouver.knows.ui.view.ViewBaseAction;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.business.bean.DiscountCategoryInfo.SubcategoryInfo;

public class ViewProject extends LinearLayout implements ViewBaseAction {

	private ListView regionListView;
	private ListView plateListView;
	private List<DiscountCategoryInfo> groups;
	private SparseArray<List<SubcategoryInfo>> children = new SparseArray<List<SubcategoryInfo>>();
	private List<SubcategoryInfo> childrenItems = new ArrayList<SubcategoryInfo>();
	private SubProjectTextAdapter plateListViewAdapter;
	private ProjectTextAdapter earaListViewAdapter;
	private OnSelectListener mOnSelectListener;
	private int tEaraPosition = 0;
	private int tBlockPosition = 0;
	private String showString;

	public ViewProject(Context context, List<DiscountCategoryInfo> categoryInfos, int index, String subCategory) {
		super(context);
		init(context, categoryInfos, index, subCategory);
	}

	private void init(Context context, List<DiscountCategoryInfo> categoryInfos, int index, String subCategory) {
		this.groups = categoryInfos;
		tEaraPosition = index;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_region, this, true);
		regionListView = (ListView) findViewById(R.id.listView);
		plateListView = (ListView) findViewById(R.id.listView2);
		setBackgroundColor(getResources().getColor(R.color.white));
		for (int i = 0; i < groups.size(); i++) {
			List<SubcategoryInfo> items = groups.get(i).getSubcategoryInfos();
			if (items == null) {
				items = new ArrayList<SubcategoryInfo>();
			}
			children.put(i, items);
		}
		earaListViewAdapter = new ProjectTextAdapter(context, groups, R.drawable.choose_project_item_selector);
		earaListViewAdapter.setSelectedPositionNoNotify(tEaraPosition);
		regionListView.setAdapter(earaListViewAdapter);
		earaListViewAdapter.setOnItemClickListener(new ProjectTextAdapter.OnItemClickListener() {

			@Override
			public void onItemClick(View view, int position) {
				if (position < children.size()) {
					tEaraPosition = position;
					childrenItems.clear();
					childrenItems.addAll(children.get(tEaraPosition));
					plateListViewAdapter.notifyDataSetChanged();

					String groupName = groups.get(position).getName();
					if ("全部商家".equals(groupName)) {
						showString = groupName;
						if (mOnSelectListener != null) {
							mOnSelectListener.getValue(showString, groups.get(tEaraPosition).getName(), "");
						}
					}
				}
			}
		});
		if (tEaraPosition < children.size())
			childrenItems.addAll(children.get(tEaraPosition));

		plateListViewAdapter = new SubProjectTextAdapter(context, childrenItems, R.drawable.choose_eara_item_selector);
		if (!StringUtil.isEmptyString(subCategory)) {
			for (int i = 0; i < childrenItems.size(); i++) {
				if (subCategory.equals(childrenItems.get(i).getName())) {
					tBlockPosition = i;
					break;
				}
			}
		}
		plateListViewAdapter.setSelectedPositionNoNotify(tBlockPosition);
		plateListView.setAdapter(plateListViewAdapter);
		plateListViewAdapter.setOnItemClickListener(new SubProjectTextAdapter.OnItemClickListener() {

			@Override
			public void onItemClick(View view, final int position) {
				tBlockPosition = position;
				showString = childrenItems.get(position).getName();
				if (StringUtil.isEmptyString(showString)) {
					showString = groups.get(tEaraPosition).getName();
				}
				if (mOnSelectListener != null) {

					mOnSelectListener.getValue(showString, groups.get(tEaraPosition).getName(),
							childrenItems.get(position).getName());
				}

			}
		});
		if (tBlockPosition < childrenItems.size()) {
			showString = childrenItems.get(tBlockPosition).getName();
		}
		if (StringUtil.isEmptyString(showString)) {
			showString = groups.get(tEaraPosition).getName();
		}
		setDefaultSelect();

	}

	public void setDefaultSelect() {
		regionListView.setSelection(tEaraPosition);
		plateListView.setSelection(tBlockPosition);
	}

	public String getShowText() {
		return showString;
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public void setSelections(String category, String subCategory) {
		regionListView.setSelection(tEaraPosition);
		plateListView.setSelection(tBlockPosition);
	}

	public interface OnSelectListener {
		public void getValue(String showText, String category, String subCategory);
	}

	@Override
	public void hide() {

	}

	@Override
	public void show() {

	}

}
